<template>
  <!-- 导航栏 -->
  <van-nav-bar title="我的订单" left-arrow @click-left="back"/>
  <van-tabs v-model:active="status" sticky @click-tab="getOrderData">
    <van-tab v-for="item in list" :title="item.title" :key="item.id" :name="item.id">
      <van-card
        v-for="item in orderlsit"
        :key="item.id"
        :price="item.amount"
        :desc="'商品名称：' + item.info.product.name"
        :title="'订单号：' + item.code"
        :thumb="item.info.product.thumb_text"
      >
        <template #tags>
          <div style="color: #969799">下单时间： {{ item.createtime }}</div>
        </template>
        <template #num>
          <van-tag type="danger">{{ item.status_text }}</van-tag>
        </template>
        <template #footer>
          <van-button type="primary" size="mini" @click="toPages(item, '/order/info')"
            >订单详情</van-button
          >
          <van-button type="success" size="mini" v-if="item.status == '0'"
            >确认支付</van-button
          >
          <van-button
            type="success"
            size="mini"
            v-if="item.status == '2'"
            @click="confirm(item)"
            >确认收货</van-button
          >
          <van-button type="danger" size="mini" v-if="item.expressid"  @click="toPages(item, '/order/express')"
            >物流详情</van-button
          >
          <van-button type="danger" size="mini" v-if="item.status > 0 && item.status != 4"
            @click="toPages(item, '/order/refund')"  >申请退款</van-button
          >
          <van-button type="warning" size="mini" v-if="item.status == '4'"  @click="toPages(item,'/order/comment')"
            >评价详情</van-button
          >
          <van-button type="danger" size="mini" v-if="item.status == '-3' && item.back_text" @click="toPages(item, '/order/refundgoods')"
            >待退货</van-button
          >

          <van-button type="warning" size="mini" v-if="item.status < 0" @click="toPages(item, '/order/refundinfo')"
            >退货详情</van-button
          >
        </template>
      </van-card>
    </van-tab>
  </van-tabs>
</template>
<script setup>
const { proxy } = getCurrentInstance();
const back = () => proxy.$router.push("/business/index");

let business = proxy.$cookies.get("business") ? proxy.$cookies.get("business") : {};
business = reactive(business);

const status = ref("");
const list = [
  {
    id: "",
    title: "全部",
  },
  {
    id: 0,
    title: "待支付",
  },
  {
    id: 1,
    title: "待发货",
  },
  {
    id: 2,
    title: "待收货",
  },
  {
    id: 3,
    title: "待评价",
  },
  {
    id: 4,
    title: "已完结",
  },
  {
    id: -1,
    title: "售后中",
  },
  {
    id: -4,
    title: "售后已结",
  },
];
const orderlsit = ref([]);
onBeforeMount(() => {
  getOrderData();
});

//获取订单信息
const getOrderData = async () => {
  let result = await proxy.$POST({
    url: "/order/index",
    params: {
      busid: business.id,
      status: status.value,
    },
  });

  if (result && result.code == 1) {
    orderlsit.value = result.data;
  }
};

//进入商品详情
const toPages = (val, url) => {
  // console.log(val);
  proxy.$router.push({
    path: url,
    query: { orderid: val.id },
  });
};

// 确认收货
const confirm = async (val) => {
  let result = await proxy.$POST({
    url: "/order/confirm",
    params: {
      busid: business.id,
      orderid: val.id,
    },
  });

  if (result && result.code == 1) {
    getOrderData();
    proxy.$success({
      message: result.msg,
    });
  } else {
    proxy.$fail(result.msg);
    return false;
  }
};
</script>
